package com.pen.changgou.goods.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.enums.SqlLike;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pen.changgou.goods.entity.Category;
import com.pen.changgou.goods.service.CategoryService;
import com.pen.changgou.common.core.utils.R;


/**
 * 商品类目
 *
 * @author yaodao
 * @email lfy@pen.changgou.com
 * @date 2020-11-29 17:35:47
 */
@Api(tags = "商品类目 管理")
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 通过ID查询菜单的详细信息
     *
     * @param id 菜单ID
     * @return 菜单详细信息
     */
    @GetMapping("/{id}")
    public R getById(@PathVariable Long id) {
        return R.ok(categoryService.getById(id));
    }

    /**
     * 列表
     */
    @ApiOperation("分页查询(排序)")
    @GetMapping("/page")
    public R<Page> pge(Page page, Category category) {
        return R.ok(categoryService.page(page, Wrappers.query(category)));

    }

    @ApiOperation("列表查询(排序)")
    @GetMapping("/list")
    public R<List<Category>> list(@RequestParam(required = false, defaultValue = "0") String parentId,  String name) {

//        String name = param.getOrDefault("name", "").toString();
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();

        if (StringUtils.isNotBlank(parentId)) {
            queryWrapper.eq("parent_id", Long.valueOf(parentId));
        }

        if (StringUtils.isNotBlank(name)) {
            queryWrapper.likeRight("name",name);
        }

        return R.ok(categoryService.list(queryWrapper));

    }


    /**
     * 信息
     */
    @ApiOperation("详情查询")
    @GetMapping("/info/{id}")

    public R<Category> info(@PathVariable("id") Long id) {
        Category category = categoryService.getById(id);

        return R.ok(category);
    }

    /**
     * 保存
     */
    @ApiOperation("保存")
    @PostMapping("/save")

    public R<Object> save(@RequestBody Category category) {
        categoryService.save(category);

        return R.ok();
    }

    /**
     * 修改
     */
    @ApiOperation("修改")
    @PostMapping("/update")
    public R<Object> update(@RequestBody Category
                                    category) {
        categoryService.updateById(category);

        return R.ok(null);
    }

    /**
     * 删除
     */
    @ApiOperation("删除")
    @PostMapping("/delete")
    public R<Object> delete(@RequestBody Long[] ids) {
        categoryService.removeByIds(Arrays.asList(ids));

        return R.ok(null);
    }

}
